package org.android.demo;

import java.io.IOException;

import org.iati.mobile.IATI;
import org.iati.mobile.IATIImpl;
import org.iati.mobile.IATIProject;
import org.iati.mobile.TransactionValue;
import org.iati.mobile.exception.IATIParseXMLException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.TextView;

public class IATIDemoProject extends Activity {
	private String IatiId;
	private ProgressDialog progressDialog;

	private IATIProject mIATIProject;

	private TextView iatiIDTextView;
	private TextView iatiTitle;
	private TextView iatiDesc;

	private String serverURL;
	private String serverPort;
	SharedPreferences pref;

	private final static String TAG = "debug";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.iatidemoproject);

		pref = PreferenceManager.getDefaultSharedPreferences(this);

		iatiIDTextView = (TextView) findViewById(R.id.iatiID);
		iatiTitle = (TextView) findViewById(R.id.iatiTitle);
		iatiDesc = (TextView) findViewById(R.id.iatiDesc);

		// Get the intent, and save the Activity ID
		Intent intent = getIntent();
		IatiId = intent.getStringExtra("iatiID");

		if (IatiId == null)
			Log.v (TAG, "IatiId is null!");
		else
			Log.v (TAG, "IatiId: " + IatiId);

		// show the progress dialog
		progressDialog = ProgressDialog.show(IATIDemoProject.this, "", "Loading...", true, true);

		final Runnable runnable = new Runnable() {
			public void run() {
				//final String iatiId = "EU-2010|22145|0";
				//final String iatiId = "41AAA-00046984";
				final String iatiId = IatiId;

				serverURL = pref.getString("url", "http://iati-collector.devgw.org");
				serverPort = pref.getString("port", "80");

				IATI mIATI = new IATIImpl(serverURL, serverPort);

				try {
					// get IATI Activity for iatiId
					Log.d(TAG, "start");
					mIATIProject = mIATI.getProjectByIatiIdentifier(iatiId);

					// update the UI
					runOnUiThread(new Runnable() {
						public void run() {
							iatiIDTextView.setText(mIATIProject.getIatiIdentifier());
							iatiTitle.setText(mIATIProject.getTitle());
							iatiDesc.setText(mIATIProject.getDescription());
						}
					});

					if (mIATIProject.getRecipientCountry() != null ) {
						Log.d(TAG, "code: " + mIATIProject.getRecipientCountry().getCode());
						Log.d(TAG, "country: " + mIATIProject.getRecipientCountry().getName());
					}

					if (mIATIProject.getTransactions() != null && mIATIProject.getTransactions().size() > 0 ) {
						Log.d(TAG, "transactionRef: " + mIATIProject.getTransactions().get(0).getTransactionRef());
						Log.d(TAG, "transactionType: " + mIATIProject.getTransactions().get(0).getTransactionType());
						Log.d(TAG, "transactionDate: " + mIATIProject.getTransactions().get(0).getTransactionDate());

						Log.d(TAG, "providerOrg: " + mIATIProject.getTransactions().get(0).getProviderOrg());
						Log.d(TAG, "receiverOrg: " + mIATIProject.getTransactions().get(0).getReceiverOrg());
						Log.d(TAG, "description: " + mIATIProject.getTransactions().get(0).getDescription());

						TransactionValue tv	= mIATIProject.getTransactions().get(0).getTransactionValue();
						if (tv != null) {
							Log.d(TAG, "value: " + tv.getValue());
							Log.d(TAG, "currency: " + tv.getCurrency());
							Log.d(TAG, "value date: " + tv.getValueDate());
						}
					}

					if (mIATIProject.getSectors() != null && mIATIProject.getSectors().size() > 0 ) {
						Log.d(TAG, "sector: " + mIATIProject.getSectors().get(0).getDescription());
						Log.d(TAG, "code: " + mIATIProject.getSectors().get(0).getCode());
						Log.d(TAG, "percentage: " + mIATIProject.getSectors().get(0).getPercentage());
						Log.d(TAG, "vocabulary: " + mIATIProject.getSectors().get(0).getVocabulary());
					}

					// dismiss the progress dialog
					if(progressDialog != null) 
						progressDialog.dismiss();
				} catch (IATIParseXMLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};

		// run on background thread.
		performOnBackgroundThread(runnable);
	}

	/**
	 * Executes the network requests on a separate thread.
	 * 
	 * @param runnable The runnable instance containing network operations to be executed.
	 */
	public static Thread performOnBackgroundThread(final Runnable runnable) {
		final Thread mThread = new Thread() {
			@Override
			public void run() {
				try {
					runnable.run();
				} finally {
					// ...
				}
			}
		};
		mThread.start();
		return mThread;
	}
}
